<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>地图选择器</title>
    <link href="/css/global.css" rel="stylesheet" type="text/css">
    <link href="all/mapSelector.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div class="main">
        <div class="mtr">
            <div class="one">
                <div class="header">
                    <span>地址选择</span>
                    <input id="searchText" type="text">
                    <div id="searchResultPanel"></div>
                    <img src="all/search.png" height="16" width="16">
                </div>
                <div id="container" class="vessel"></div>
                <div class="btnBox">
                    <div class="tip">
                        <span class="none">您有一次修改地址的机会！</span>
                        <input type="text">
                    </div>
                    <div class="fr">
                        <button class="btn btn-submit editAdd">确定</button>
                        <button class="btn btn-return">返回</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="../jquery-1.11.1.min.js"></script>
    <script src="../config.js"></script>
    <script src="../ly_main.js"></script>
    <script src="../gps.js"></script>
    <script src="//api.map.baidu.com/getscript?v=2.0&ak=6NSf3ER1QWCV8HuCIDVVwLrslmRHjD65&services=&t=20171031174121"></script>
    <script type="text/javascript">
        //初始化地图marker的信息
        var inInfo = parent.$('.singleMapSelector-iframe').attr('ininfo');
        var map = new BMap.Map("container",{enableMapClick:false});          // 创建地图实例
        var point = new BMap.Point(106.53063501,29.54460611);  // 创建点坐标
        var geoc = new BMap.Geocoder();               // 创建地理编码实例 
        var infoFlag = '';                            //详细地址
        var marker = new BMap.Marker(point),nowPoint = {lng:'',lat:''};          //地图地点标记和经纬度
        if(inInfo && inInfo != '{}') {
            var jsonData = JSON.parse(inInfo);
            if(jsonData.lng && jsonData.lat) {
                /*百度转高德*/
                // var gdtodb = GPS.bd_encrypt(jsonData.lat,jsonData.lng)
                // var inPoint = new BMap.Point(gdtodb.lon,gdtodb.lat);
                var inPoint = new BMap.Point(jsonData.lng,jsonData.lat);
                marker = new BMap.Marker(inPoint);
                addMarker(inPoint);
                infoFlag = jsonData.address;
                $(".tip input").val(infoFlag);
                nowPoint = inPoint;
                map.centerAndZoom(inPoint, 15);
            } else {
                map.centerAndZoom(point, 15); 
            }
        } else {
            map.centerAndZoom(point, 15);                 // 初始化地图，设置中心点坐标和地图级别
            var geolocation = new BMap.Geolocation();
            geolocation.getCurrentPosition(function(r){
                if(this.getStatus() == BMAP_STATUS_SUCCESS){
                    var mk = new BMap.Marker(r.point);
                    //map.addOverlay(mk);
                    map.panTo(r.point);
                }
            })
        }
        map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
        map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用
        // 传出数据
        var comeOut = {
            address: infoFlag,
            lng: nowPoint.lng,
            lat: nowPoint.lat
        };
        parent.$('.singleMapSelector-iframe').data('outInfo', JSON.stringify(comeOut));
        var ac = new BMap.Autocomplete({    //建立一个自动完成的对象
            "input" : "searchText",
            "location" : map
        });

        ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
            var str = "";
            var _value = e.fromitem.value;
            var value = "";
            if (e.fromitem.index > -1) {
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }    
            str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }    
            str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
            $("#searchResultPanel").innerHTML = str;
        });

        var myValue;
        ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
            var _value = e.item.value;
            myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            infoFlag = myValue;
            $("#searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
            setPlace();
        });

        function setPlace(){
            map.clearOverlays();    //清除地图上所有覆盖物
            function myFun(){
                nowPoint = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                map.centerAndZoom(nowPoint, 15);
                marker = new BMap.Marker(nowPoint);
                // var bdtogd =  GPS.bd_decrypt(nowPoint.lat,nowPoint.lng);
                comeOut.address = infoFlag;
                comeOut.lng = nowPoint.lng;
                comeOut.lat = nowPoint.lat;
                parent.$('.singleMapSelector-iframe').data('outInfo', JSON.stringify(comeOut));
                $(".tip input").val(infoFlag);
                map.addOverlay(marker);    //添加标注
                // marker.enableDragging();
            }
            var local = new BMap.LocalSearch(map, { //智能搜索
                onSearchComplete: myFun
            });
            local.search(myValue);
        }

        // 设置标注
        function addMarker(point){
            map.clearOverlays();
            marker = new BMap.Marker(point);
            map.addOverlay(marker);
            // marker.enableDragging();
        }
        //单击获取点击的经纬度
        map.addEventListener("click",function(e){
            if(e.overlay){
                var infoWindow = new BMap.InfoWindow(infoFlag, {width: 200,height: 50});
                map.openInfoWindow(infoWindow,nowPoint);
            }else{
                nowPoint = new BMap.Point(e.point.lng,e.point.lat);
                geoc.getLocation(e.point, function(rs){
                    var addComp = rs.addressComponents;
                    infoFlag = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                    // var bdtogd =  GPS.bd_decrypt(nowPoint.lat,nowPoint.lng);
                    comeOut.address = infoFlag;
                    comeOut.lng = nowPoint.lng;
                    comeOut.lat =nowPoint.lat;
                    parent.$('.singleMapSelector-iframe').data('outInfo', JSON.stringify(comeOut));
                    $(".tip input").val(infoFlag);
                    addMarker(nowPoint);
                }); 
            }
        });
        $('.btnBox').on('click', '.editAdd', function(){
            if(!comeOut.lng || !comeOut.lat) {
                ly.toast('您尚未选择地址');
                return;
            }
            $(".tip input").animate({width:"310px",paddingLeft:"10px",paddingRight:"10px"},200);
            setTimeout(function(){
                $('.tip span').removeClass('none');
                $(".tip input").show();
                $('.one .btn-submit').removeClass('editAdd').addClass('submit');
            },300);
        });
    </script>
</body>
</html>












